package com.example.administrator.framework.core;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.example.administrator.framework.R;
import com.example.administrator.framework.widgets.pull.PullToRefreshRecycler;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017-04-07.
 */

public abstract class BaseListActivity<T> extends BaseActivity  implements PullToRefreshRecycler.OnRecyclerRefreshListener {


    protected BaseListAdapter adapter;
    protected ArrayList<T> mDataList = new ArrayList<>();
    protected PullToRefreshRecycler recycler;

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_base_list, -1);
    }

    @Override
    protected void setUpView() {
        recycler = (PullToRefreshRecycler) findViewById(R.id.pullToRefreshRecycler);
    }

    @Override
    protected void setUpData() {
        recycler.setOnRefreshListener(this);
        recycler.setLayoutManager(getLayoutManager());
        recycler.addItemDecoration(getItemDecoration());
        adapter = new BaseListAdapter();
        recycler.setAdapter(adapter);
    }

    protected RecyclerView.ItemDecoration getItemDecoration() {
        return new DividerItemDecoration(getApplicationContext(), R.drawable.list_divider);
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getApplicationContext());
    }


    public class BaseListAdapter extends RecyclerView.Adapter<BaseViewHolder> {

        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return getViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            holder.onBindViewHolder(position);
        }


        @Override
        public int getItemCount() {
            return mDataList != null ? mDataList.size() : 0;
        }

        @Override
        public int getItemViewType(int position) {
            return getItemType(position);
        }
    }


    protected abstract BaseViewHolder getViewHolder(ViewGroup parent, int viewType);

    protected int getItemType(int position) {
        return 0;
    }
}
